package cn.com.yx.ms.controller;

import cn.com.yx.ms.domain.GlobalConfigModel;
import cn.com.yx.ms.domain.sys.MoveCommonRequestModel;
import cn.com.yx.ms.domain.sys.ValidImgModel;
import cn.com.yx.ms.service.ICommonService;
import cn.com.yx.ms.service.ISysService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "sys")
@Api(value = "系统参数接口", description = "用于获取系统配置相关信息")
public class SysController {

    private static final Logger logger = LoggerFactory.getLogger(SysController.class);

    @Resource
    private ISysService iSysService;
    @Resource
    private ICommonService iCommonService;


    @PostMapping("global/config")
    @ApiOperation(value = "保存全局配置", notes = "保存全局配置")
    @ApiImplicitParam(paramType = "body", name = "globalConfigModel", dataType = "Map", required = true, value = "保存全局配置信息")
    public ApiResult saveConfig(@RequestBody @Validated GlobalConfigModel globalConfigModel, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.saveConfig(globalConfigModel));
    }


    @ResponseBody
    @RequestMapping(value = "global/config", method = GET)
    @ApiOperation("全局配置资源")
    public ApiResult findById() {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findGlobalConfig());
    }

    @RequestMapping(value = "/captchaImage", method = GET)
    public ValidImgModel getKaptchaImage(HttpServletResponse response) throws Exception {
        return iSysService.gengrateValidCode(response);
    }

    @RequestMapping(value = "/getValidCode")
    public String getValidCode(HttpServletRequest request) {
        return iSysService.getValidCode(request.getParameter("uuid"));
    }

    @PostMapping("common/model/move")
    @ApiOperation(value = "对象移动", notes = "对象移动")
    public ApiResult moveObject(@RequestBody @Validated MoveCommonRequestModel moveModel) {
        ApiCode apiCode = iCommonService.moveOrderIndex(moveModel.getTable(),
                moveModel.getDirect(),
                moveModel.getGuid(),
                moveModel.getParentid());
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_300500),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }



}